#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;

int A[] = {1,2,2,4};
const int N = sizeof(A) / sizeof(int);

template <class BidirectionalIterator> 
void snail_sort(BidirectionalIterator first, BidirectionalIterator last)
{
  while (next_permutation(first, last)) {
	  copy(A, A+N, ostream_iterator<int>(cout, " "));
	  cout<<"\n";
  }
}

int main()
{

	snail_sort(A, A+N);
}

